<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>登录页面</title>
    <link href="css/common.css" rel="stylesheet">
    <link href="css/login.css" rel="stylesheet">
</head>
<body>
    <input type="text" id="message">
    <button id="send-button">发送</button>

    <script>
        //js使用websocket代码
        //创建一个websocket实例
        let webSocket = new WebSocket('ws://127.0.0.1:8080/test')
        //连接完成自动执行
        webSocket.onopen = function () {
            console.log('websocket 连接成功')
        }
        //收到消息自动执行
        webSocket.onmessage = function (e) {
            console.log('websocket 收到消息，消息是：'+e.data)
        }
        //连接关闭自动执行
        webSocket.onclose = function () {
            console.log('websocket 连接关闭')
        }
        //连接异常自动执行
        webSocket.onerror = function () {
            console.log('websocket 连接异常')
        }

        //给按钮注册点击事件
        let button = document.querySelector('#send-button')
        let messageInput = document.querySelector('#message')
        button.onclick = function () {
            console.log('websocket 发送信息：' + messageInput.value)
            webSocket.send(messageInput.value)
        }
    </script>
</body>
</html>